package com.dfcj.oauth2server.configure;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

import javax.servlet.http.HttpServletResponse;

@Configuration
@EnableResourceServer
public class ResourceServerConfig  extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        /*
        antMatchers("/actuator/**", "/oauth/**","/token/**").permitAll()
        表示这个不经过，basic认证
         */

//        http
//                     .requestMatchers().anyRequest()
//                     .and()
//                         .authorizeRequests()
//                         .antMatchers("/oauth/*").permitAll();
        http
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint((request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED))
                .and()
                .authorizeRequests()
                .antMatchers("/actuator/**", "/oauth/**","/token/**").permitAll()
                .anyRequest().authenticated()
                .and()
//                .httpBasic()
//                .and()
                .formLogin().permitAll();
    }
}
